package cn.tqfeiyang.smartadmin.commons.toolbox;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 泛型工具
 *
 * @author tqfeiyang
 * @since 2024/10/7 10:03
 */
public class GenericTypeUtils {

    private GenericTypeUtils() {}

    public static List<Class<?>> resolveTypeArguments(Class<?> clazz) {
        Type[] genericInterfaces = clazz.getGenericInterfaces();
        if (genericInterfaces.length > 0 && genericInterfaces[0] instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) genericInterfaces[0];
            Type[] actualTypeArguments = pt.getActualTypeArguments();
            List<Class<?>> argList = new ArrayList<>();
            Arrays.stream(actualTypeArguments).forEach(type -> argList.add((Class<?>) type));
            return argList;
        } else {
            return Arrays.asList();
        }
    }

}
